//
//  CYYGuideViewController.m
//  LetsParty
//
//  Created by YG on 15/9/24.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYGuideViewController.h"

@interface CYYGuideViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView * guidScrollView;//引导
@property (nonatomic, strong) UIImageView * firstImageView;
@property (nonatomic, strong) UIImageView * secondImageView;
@property (nonatomic, strong) UIImageView * thirdImageView;
@property (nonatomic, strong) UIButton * nextButton;//进入app按钮
@property (nonatomic, strong) UIPageControl * cPageControl;//分页控件

- (void)initializeUserInterface;

@end

@implementation CYYGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}

#pragma mark - system protrol implenments
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.cPageControl.currentPage = (NSInteger )scrollView.contentOffset.x / 320;
}

#pragma mark - private methods
- (void)initializeUserInterface{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.guidScrollView];
    [self.guidScrollView addSubview:self.firstImageView];
    [self.guidScrollView addSubview:self.secondImageView];
    [self.guidScrollView addSubview:self.thirdImageView];
    [self.thirdImageView addSubview:self.nextButton];
    [self.view addSubview:self.cPageControl];
    [self.nextButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (UIImageView *)haveGuideViewIndex:(NSInteger)index{
    NSArray * imageNameArray = @[@"guide1",@"guide2",@"guide3"];
    UIImageView * cImageView = [[UIImageView alloc]init];
    cImageView.frame = DHFlexibleFrame(CGRectMake(320 * (index - 1), 0, 320, 568), NO);
    cImageView.image = [UIImage imageNamed:imageNameArray[index - 1]];
    cImageView.userInteractionEnabled = YES;
    return cImageView;
}

#pragma mark - responds to event
- (void)respondsToButton:(UIButton *)button{
    //将版本号存进本地
    [[NSUserDefaults standardUserDefaults] setObject:[UserModel defaultUser].versionString forKey:@"version"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self presentViewController:[[ControllerManager sharedManager] rootViewController] animated:YES completion:nil];
}

- (void)respondsToPageControl:(UIPageControl *)pageControl{
    self.guidScrollView.contentOffset = DHFlexibleCenter(CGPointMake(320 * self.cPageControl.currentPage, 0));
//    NSLog(@"%ld",pageControl.currentPage);
}

- (void)updateCurrentPageDisplay{
    
}

#pragma mark -getter
- (UIScrollView *)guidScrollView{
    if (!_guidScrollView) {
        _guidScrollView = ({
            UIScrollView * cScrollView = [[UIScrollView alloc]init];
            cScrollView.frame = self.view.frame;
            cScrollView.contentSize = DHFlexibleSize(CGSizeMake(320 * 3, 568), NO);
            cScrollView.showsHorizontalScrollIndicator = NO;
            cScrollView.bounces = NO;
            cScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
            cScrollView.pagingEnabled = YES;
            cScrollView.delegate = self;
            cScrollView;
        });
    }
    return _guidScrollView;
}

- (UIImageView *)firstImageView{
    if (!_firstImageView) {
        _firstImageView = ({
            [self haveGuideViewIndex:1];
        });
    }
    return _firstImageView;
}

- (UIImageView *)secondImageView{
    if (!_secondImageView) {
        _secondImageView = ({
            [self haveGuideViewIndex:2];
        });
    }
    return _secondImageView;
}

- (UIImageView *)thirdImageView{
    if (!_thirdImageView) {
        _thirdImageView = ({
            [self haveGuideViewIndex:3];
        });
    }
    return _thirdImageView;
}

- (UIButton *)nextButton{
    if (!_nextButton) {
        _nextButton = ({
            UIButton * cButton = [UIButton buttonWithType:UIButtonTypeCustom];
            cButton.frame = DHFlexibleFrame(CGRectMake(85, 450, 150, 40), NO);
            [cButton setTitle:@"进入Let's Party" forState:UIControlStateNormal];
            cButton.layer.borderColor = [UIColor whiteColor].CGColor;
            cButton.layer.borderWidth = 0.5f;
            cButton.layer.cornerRadius = 20 * DHFlexibleVerticalMutiplier();
            cButton.layer.masksToBounds = YES;
            cButton;
        });
    }
    return _nextButton;
}

- (UIPageControl *)cPageControl{
    if (!_cPageControl) {
        _cPageControl = ({
            UIPageControl * cPageControl = [[UIPageControl alloc]init];
            cPageControl.frame = DHFlexibleFrame(CGRectMake(0, 500, 320, 50), NO);
            cPageControl.tintColor = [UIColor whiteColor];
            cPageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
            cPageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
            cPageControl.numberOfPages = 3;
            [cPageControl addTarget:self action:@selector(respondsToPageControl:) forControlEvents:UIControlEventValueChanged];
            cPageControl;
        });
    }
    return _cPageControl;
}

@end
